package com.junmeng.libfloating.ball

import android.view.Gravity
import com.junmeng.libfloating.R

class BallConfig {
    companion object {
        @JvmStatic
        fun newBuilder(father: SimpleFloatingViewFactory.Builder): Builder {
            return Builder(father)
        }

    }

    class Builder(private var father: SimpleFloatingViewFactory.Builder) {

        var layoutResId: Int = R.layout.floating_ball

        //        var iconResId: Int = android.R.mipmap.sym_def_app_icon
        var iconResId: Int = R.drawable.floating_baseline_toys_24

        var backgroundSelector: Array<Int> = arrayOf(
            R.drawable.floating_shape_ball_bg_start,
            R.drawable.floating_shape_ball_bg_middle,
            R.drawable.floating_shape_ball_bg_end
        )

        var initGravity: Int = Gravity.START


        var idleDelayMs = 2000L //进入空闲状态的时间ms
        var idleAlpha = 0.1f //空闲状态时的透明度

        var idleWidth = 0.67f //空闲时的宽度占原宽度的比例，如果大于1，则为空闲时展示的宽度

        var marginTop = 0 //悬浮球距离顶部的距离，此设置也会在悬浮面板生效
        var marginBottom = 0
        var marginLeft = 0
        var marginRight = 0

        /**
         * 悬浮球距离顶部的距离，此设置也会在悬浮面板中生效
         * @param top Int
         * @return Builder
         */
        fun marginTop(top: Int): Builder {
            marginTop = top
            return this
        }

        fun marginBottom(bottom: Int): Builder {
            marginBottom = bottom
            return this
        }

        fun marginLeft(left: Int): Builder {
            marginLeft = left
            return this
        }

        fun marginRight(right: Int): Builder {
            marginRight = right
            return this
        }

        /**
         * 悬浮球空闲时的显示宽度
         * @param width Float 如果大于1，则为空闲时展示的宽度 如果小于1，则为占原悬浮球宽度的比例
         * @return Builder
         */
        fun ballIdleShowWidth(width: Float): Builder {
            if (width > 0) {
                idleWidth = width
            }
            return this
        }


        /**
         * 悬浮球多长时间无操作进入空闲状态
         * @param ms Long
         * @return Builder
         */
        fun ballEnterIdleTimeMs(ms: Long): Builder {
            if (ms > 0) {
                idleDelayMs = ms
            }
            return this
        }

        /**
         * 悬浮球进入无操作时的显示透明度
         * @param alpha Float 0.0~1.0之间 值越小越透明
         * @return Builder
         */
        fun ballAlphaWhenIdle(alpha: Float): Builder {
            if (alpha > 0f && alpha <= 1.0f) {
                idleAlpha = alpha
            }
            return this
        }

        /**
         * 自定义悬浮球视图
         */
        fun viewResId(layoutResId: Int): Builder {
            this.layoutResId = layoutResId
            return this
        }

        /**
         * 悬浮球图标
         */
        fun iconResId(iconResId: Int): Builder {
            this.iconResId = iconResId
            return this
        }

        /**
         * 悬浮球背景数组，下标0表示停靠在左侧时的背景，1表示在拖拽时的背景，2表示停靠在右侧时的背景
         * 注意数组数量必须为3，否则不会生效
         */
        fun backgroundSelector(backgroundSelector: Array<Int>): Builder {
            if (backgroundSelector.size != 3) {
                return this
            }
            this.backgroundSelector = backgroundSelector
            return this
        }

        /**
         * 用户可据此设置初始位置，默认在做上角，如果layoutParams设置的话则此设置无效，即layoutParams有设置的话则以layoutParams设置的为准
         * 如果想左侧居中，则传android.view.Gravity.CENTER_VERTICAL
         * 如果想左下角，则传android.view.Gravity.BOTTOM
         * 如果想右上角，则传android.view.Gravity.END
         * 如果想右侧居中，则传android.view.Gravity.END|android.view.Gravity.CENTER_VERTICAL
         * 如果想右下角，则传android.view.Gravity.END|android.view.Gravity.BOTTOM
         * 注意使用kotlin的话则“|”要改为or
         */
        fun initGravity(gravity: Int): Builder {
            this.initGravity = gravity
            return this
        }

        /**
         * 完成配置
         */
        fun finish(): SimpleFloatingViewFactory.Builder {
            return father
        }
    }


}